//
//  WKFriendBaseView.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/20.
//  Copyright © 2018 何文虎. All rights reserved.
//

#import "WKFriendBaseView.h"
#import "WKPlanBookCollectCell.h"
#import "WKContentListModel.h"

@interface WKFriendBaseView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
@property (nonatomic, strong) NSIndexPath *lastSelectedIndexPath;
@property(nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation WKFriendBaseView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _isHeaderRefreshed = YES;
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:UITableViewStylePlain];
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.tableFooterView = [UIView new];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        [self.tableView registerClass:[WKPlanBookCollectCell class] forCellReuseIdentifier:NSStringFromClass([WKPlanBookCollectCell class])];
        [self addSubview:self.tableView];
        
        
        
        [self updata];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.tableView.frame = self.bounds;
}

- (void)beginFirstRefresh {
    if (!self.isHeaderRefreshed) {
        [self beginRefreshImmediately];
    }
}

- (void)beginRefreshImmediately {
    [self.tableView.mj_header beginRefreshing];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.isHeaderRefreshed = YES;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    });
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataSource.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WKPlanBookCollectCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WKPlanBookCollectCell class]) forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat height = 0;
    WKContentListModel *model = [self.dataSource objectAtIndex:indexPath.row];
    height = [WKPlanBookCollectCell hyb_heightForTableView:tableView config:^(UITableViewCell *sourceCell) {
        WKPlanBookCollectCell *cell = (WKPlanBookCollectCell*) sourceCell;
        cell.model = model;
        
    } cache:^NSDictionary *{
        return @{kHYBCacheUniqueKey: model.contentId,
                 kHYBCacheStateKey : @"",
                 // 如果设置为YES，若有缓存，则更新缓存，否则直接计算并缓存
                 // 主要是对社交这种有动态评论等不同状态，高度也会不同的情况的处理
                 kHYBRecalculateForStateKey : @(YES) // 标识不用重新更新
                 };
    }];
    
    
    return height;
}

- (void)updata{
    NSDictionary *dic = @{@"type":@"0",@"headerImagUrl":@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544598563&di=2411d1f0f5d99380c76dc0d3c04bb138&imgtype=jpg&er=1&src=http%3A%2F%2Ftvax2.sinaimg.cn%2Fcrop.0.0.667.667.1024%2F0077xMUHly8fru1jjd5rnj30ij0ijab3.jpg",@"name":@"会飞的鱼儿",@"membe":@"精英会员",@"content":@"《夜曲》的MV主线是周杰伦扮演的男子思念死去女友的故事，MV的故事也是周杰伦自己构思的，MV中将为失去的女主角弹奏她生前最爱的肖邦的《夜曲》。MV的导演是邝盛，他对周杰伦在MV中的表演表示肯定。而周杰伦本人则表示：“这首歌用死亡来表现歌曲的张力。”《夜曲》的棚内部分是在台湾完成的，为了保持一致拍摄时将当时的服装全部找出来让周杰伦穿上，还在拍摄现场搭起了类似纽约民居的场景，拍摄时回忆及一年前的情景，不禁开玩笑感叹自己走进了时光隧道。",@"images":@[@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544002093712&di=97ffda895319de384ac0643e43eb79e7&imgtype=0&src=http%3A%2F%2Fcdn.pingwest.com%2Fwp-content%2Fuploads%2F2016%2F05%2Fzhoujielun.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544002115813&di=c7f9d0be4008644b5e93294d16db97b4&imgtype=0&src=http%3A%2F%2Fdingyue.nosdn.127.net%2F5wGbhURl1KfsTvPS8FqZgqffrVq2DWFCFqtubkZymjDzf1542294767536.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544002115811&di=795e9941ac71a0a94f7b242b4fb6801b&imgtype=0&src=http%3A%2F%2Fs9.rr.itc.cn%2Fr%2FwapChange%2F20165_25_10%2Fa3fq7o76823521039596.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544002175623&di=ffd387d0061a82089e0dc200729e47f5&imgtype=0&src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farchive%2F102db6e8ad0be68b5f10d034e58b04cb6f2f94a8.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544006938715&di=d52a35e0f3bfbd096a81e707e90aff1c&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201508%2F11%2F20150811175706_mrEyn.jpeg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544006938589&di=7c44b5c5b142a4e6b4f5bc686757510e&imgtype=0&src=http%3A%2F%2Fgss0.baidu.com%2F-4o3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2F6a600c338744ebf806d67ca9dbf9d72a6159a784.jpg"],@"video":@"https://xingchenxueyuan.oss-cn-beijing.aliyuncs.com/20180521/%E5%B0%8F%E6%B0%94%E6%B3%A1%E6%88%90%E7%89%87%E6%AD%A3%E5%B8%B8%E8%82%8C%E8%82%A4%E6%B5%81%E7%A8%8B.mp4",@"comment":@"流行结合古典的夜曲，哥的最爱，哥真正入坑专！吸粉最多的一张专辑，范特西卖的那么好，十一月的肖邦居功至伟！因为周当时的女朋友爱听慢情歌，所以这张专辑比前5张专辑多",@"questionStr":@"全球最大的周杰伦网站!是一家以提供周杰伦相关信息为主的专业网站,成立于2001年11月9日。主要包括周杰伦新闻、资料、图片"};
    
    self.dataSource = nil;
    if (self.dataSource == nil) {
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    for (int i =0; i<20; i++) {
        WKContentListModel *model = [WKContentListModel mj_objectWithKeyValues:dic];
        model.contentId = IntToString(i);
        int selectIndex = (int)(1 + (arc4random() % (6 -1 + 1)));
        NSMutableArray *randomArr = [NSMutableArray arrayWithCapacity:0];
        for (int i = 0; i<selectIndex; i++) {
            [randomArr addObject:[NSString stringWithFormat:@"%@", model.images[i]]];
        }
        model.images = [NSArray arrayWithArray:randomArr];
        if (selectIndex%2==0) {
            model.comment = @"";
            
            
        }
        
        
        
        [self.dataSource addObject:model];
    }
    
    [self.tableView reloadData];
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    !self.scrollCallback ?: self.scrollCallback(scrollView);
}

#pragma mark - JXPagingViewListViewDelegate

- (UIView *)listView {
    return self;
}

- (UIScrollView *)listScrollView {
    return self.tableView;
}

- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
    self.scrollCallback = callback;
}

@end
